package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody Map<String,String> map) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = simpleDateFormat.parse(map.get("orderDate"));
        //判断是否是未来时间
        if(orderDate.getTime()<System.currentTimeMillis()){
            return new Result(false,MessageConst.ORDERSETTING_FAIL);
        }
        int num = Integer.parseInt(map.get("number"));
        //先查
        OrderSetting orderSetting = orderSettingService.selectByDate(orderDate);
        //如果有对象 且已预约数字>传来的数字，return设置失败
        if(orderSetting!=null){
            if(orderSetting.getReservations()>num) {
                return new Result(false,MessageConst.ORDERSETTING_FAIL);
            }else {
                //大于，进行更新
                orderSetting.setNumber(num);
                orderSettingService.update(orderSetting);
                return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
            }
        }
        //为null save
        if(orderSetting==null){
            OrderSetting setting = new OrderSetting(orderDate,num);
            orderSettingService.save(setting);
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
        }
        return new Result(false,MessageConst.ORDERSETTING_FAIL);

    }

    /**
     * excel文件导入控制器
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //读取excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size()>0){
                List<OrderSetting> orderSettings = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]),
                            Integer.parseInt(strings[1]));
                    orderSettings.add(orderSetting);
                }
                orderSettingService.add(orderSettings);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS);
    }

    /**
     * 根据月份查询当月预约设置信息
     * @param year
     * @param month
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("year") String year,
                                         @RequestParam("month") String month){
        try {
            String date = year+"-"+month;
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //成功获取预约设置信息
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //获取失败
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }
}
